from django.views.generic import ListView, DetailView
from django.views.generic.edit import UpdateView, DeleteView, CreateView
from django.core.urlresolvers import reverse_lazy

from business.models import Country


class CountryListView(ListView):
    queryset = Country.objects.all().order_by('name')
    paginate_by = 10


class CountryDetailView(DetailView):
    model = Country
    # Generic views like DetailView also include self.request
    # and self.request.args which is like ruby's params and stores
    # argument values captured by the URLconf regular expressions,
    # and self.kwargs as well which holds name-based captured args as opposed
    # to positional by self.args.

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(CountryDetailView, self).get_context_data(**kwargs)
        # Add counties to context
        county_list = self.object.county_set.order_by('name')
        context['county_list'] = county_list
        return context


# Create
class CountryCreateView(CreateView):
    model = Country
    template_name_suffix = '_create_form'  # country_create_form.html


# Update
class CountryUpdateView(UpdateView):
    model = Country
    template_name_suffix = '_update_form'


# Delete
class CountryDeleteView(DeleteView):
    model = Country
    template_name_suffix = '_delete_form'
    success_url = reverse_lazy('country_listview')  # uses reverse_lazy because the urls haven't been loaded at this time
